home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / ZMISC.C < prev    next >
C/C++ Source or Header  |  1991-09-29  |  7KB  |  238 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zmisc.c */
  21. /* Miscellaneous operators for Ghostscript */
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "ghost.h"
  25. #include "gp.h"
  26. #include "errors.h"
  27. #include "oper.h"
  28. #include "alloc.h"
  29. #include "dict.h"
  30. #include "name.h"
  31. #include "packed.h"
  32. #include "store.h"
  33. #include "gxfixed.h"            /* for gstype1.h */
  34. #include "gstype1.h"
  35.  
  36. /* Import the C getenv function */
  37. extern char *getenv(P1(char *));
  38.  
  39. /* bind */
  40. int
  41. zbind(register os_ptr op)
  42. {    os_ptr bsp = op;            /* bottom of stack */
  43.     ref *defp = op;
  44.     switch ( r_type(op) )
  45.        {
  46.     case t_array:
  47.     case t_mixedarray:
  48.     case t_shortarray:
  49.         break;
  50.     case t_oparray:
  51.         defp = &op_array_table.value.refs[op_index(op) - op_def_count];
  52.         break;
  53.     default:
  54.         return e_typecheck;
  55.        }
  56.     ++bsp;
  57.     /* We must not make the top-level procedure read-only, */
  58.     /* but we must bind it even if it is read-only already. */
  59.     *bsp = *defp;
  60.     /* Here are the invariants for the following loop: */
  61.     /*    op < bsp <= ostop; */
  62.     /*    for every pointer p such that op < p <= bsp, */
  63.     /*      *p is an array (or packedarray) ref. */
  64. #define r_is_ex_oper(rp)\
  65.   ((r_btype(rp) == t_operator || r_type(rp) == t_oparray) &&\
  66.    r_has_attr(rp, a_executable))
  67.     while ( bsp > op )
  68.        {    while ( r_size(bsp) )
  69.            {    ref *tp = bsp->value.refs;
  70.             r_inc_size(bsp, -1);
  71.             if ( *(ushort *)tp > packed_max_full_ref )
  72.              { /* Check for a packed executable name */
  73.                ushort elt = *(ushort *)tp;
  74.                if ( (elt & ~(ushort)packed_max_name_index) ==
  75.                 pt_tag(pt_executable_name) )
  76.                 { ref nref;
  77.                   ref *pvalue;
  78.                   name_index_ref(elt & packed_max_name_index,
  79.                          &nref);
  80.                   if ( (pvalue = dict_find_name(&nref)) != 0 &&
  81.                    r_is_ex_oper(pvalue)
  82.                  )
  83.                 /* Note: can't undo this by restore! */
  84.                 *(ushort *)tp =
  85.                   pt_tag(pt_executable_operator) +
  86.                   op_index(pvalue);
  87.                 }
  88.                bsp->value.refs = (ref *)((ushort *)tp + 1);
  89.              }
  90.             else
  91.               switch ( bsp->value.refs++, r_type(tp) )
  92.              {
  93.             case t_name:    /* bind the name if an operator */
  94.               if ( r_has_attr(tp, a_executable) )
  95.                {    ref *pvalue;
  96.                 if ( (pvalue = dict_find_name(tp)) != 0 &&
  97.                      r_is_ex_oper(pvalue)
  98.                    )
  99.                     ref_assign_old(tp, pvalue, "bind");
  100.                }
  101.               break;
  102.             case t_array:    /* push into array if procedure */
  103.               if ( !r_has_attr(tp, a_write) ) break;
  104.             case t_mixedarray:
  105.             case t_shortarray:
  106.               if ( r_has_attr(tp, a_executable) && bsp < ostop )
  107.                {    /* Make reference read-only */
  108.                 r_clear_attrs(tp, a_write);
  109.                 *++bsp = *tp;
  110.                }
  111.              }
  112.            }
  113.         bsp--;
  114.        }
  115.     return 0;
  116. }
  117.  
  118. /* currenttime */
  119. int
  120. zcurrenttime(register os_ptr op)
  121. {    long date_time[2];
  122.     gp_get_clock(date_time);
  123.     push(1);
  124.     make_real(op, date_time[0] * 1440.0 + date_time[1] / 60000.0);
  125.     return 0;
  126. }
  127.  
  128. /* getenv */
  129. int
  130. zgetenv(register os_ptr op)
  131. {    char *str, *value;
  132.     int code;
  133.     check_read_type(*op, t_string);
  134.     str = ref_to_string(op, "getenv name");
  135.     if ( str == 0 ) return e_VMerror;
  136.     value = getenv(str);
  137.     alloc_free(str, r_size(op) + 1, 1, "getenv name");
  138.     if ( value == 0 )        /* not found */
  139.        {    make_bool(op, 0);
  140.         return 0;
  141.        }
  142.     code = string_to_ref(value, op, "getenv value");
  143.     if ( code < 0 ) return code;
  144.     push(1);
  145.     make_bool(op, 1);
  146.     return 0;
  147. }
  148.  
  149. /* makeoperator */
  150. int
  151. zmakeoperator(register os_ptr op)
  152. {    check_type(op[-1], t_name);
  153.     check_proc(*op);
  154.     if ( op_array_count == r_size(&op_array_table) )
  155.         return e_limitcheck;
  156.     ref_assign_old(&op_array_table.value.refs[op_array_count],
  157.                op, "makeoperator");
  158.     op_array_nx_table[op_array_count] = name_index(op - 1);
  159.     r_set_type_attrs(op - 1, t_oparray, a_executable);
  160.     r_set_size(op - 1, op_def_count + op_array_count);
  161.     op_array_count++;
  162.     pop(1);
  163.     return 0;
  164. }
  165.  
  166. /* setdebug */
  167. int
  168. zsetdebug(register os_ptr op)
  169. {    check_read_type(op[-1], t_string);
  170.     check_type(*op, t_boolean);
  171. #ifdef DEBUG
  172.        {    int i;
  173.         for ( i = 0; i < r_size(op - 1); i++ )
  174.             gs_debug[op[-1].value.bytes[i] & 127] =
  175.                 op->value.index;
  176.        }
  177. #endif
  178.     pop(2);
  179.     return 0;
  180. }
  181.  
  182. /* type1encrypt, type1decrypt */
  183. private int type1crypt(P2(os_ptr,
  184.               int (*)(P4(byte *, byte *, uint, ushort *))));
  185. int
  186. ztype1encrypt(os_ptr op)
  187. {    return type1crypt(op, gs_type1_encrypt);
  188. }
  189. int
  190. ztype1decrypt(os_ptr op)
  191. {    return type1crypt(op, gs_type1_decrypt);
  192. }
  193. private int
  194. type1crypt(register os_ptr op, int (*proc)(P4(byte *, byte *, uint, ushort *)))
  195. {    crypt_state state;
  196.     uint ssize;
  197.     check_type(op[-2], t_integer);
  198.     state = op[-2].value.intval;
  199.     if ( op[-2].value.intval != state )
  200.         return e_rangecheck;    /* state value was truncated */
  201.     check_read_type(op[-1], t_string);
  202.     check_write_type(*op, t_string);
  203.     ssize = r_size(op - 1);
  204.     if ( r_size(op) < ssize )
  205.         return e_rangecheck;
  206.     (void) (*proc)(op->value.bytes, op[-1].value.bytes, ssize,
  207.                &state);        /* can't fail */
  208.     op[-2].value.intval = state;
  209.     op[-1] = *op;
  210.     r_set_size(op - 1, ssize);
  211.     pop(1);
  212.     return 0;
  213. }
  214.  
  215. /* usertime */
  216. int
  217. zusertime(register os_ptr op)
  218. {    long date_time[2];
  219.     gp_get_clock(date_time);
  220.     push(1);
  221.     make_int(op, date_time[0] * 86400000L + date_time[1]);
  222.     return 0;
  223. }
  224.  
  225. /* ------ Initialization procedure ------ */
  226.  
  227. op_def zmisc_op_defs[] = {
  228.     {"1bind", zbind},
  229.     {"0currenttime", zcurrenttime},
  230.     {"1getenv", zgetenv},
  231.     {"2makeoperator", zmakeoperator},
  232.     {"2setdebug", zsetdebug},
  233.     {"3type1encrypt", ztype1encrypt},
  234.     {"3type1decrypt", ztype1decrypt},
  235.     {"0usertime", zusertime},
  236.     op_def_end(0)
  237. };
  238.